package me.zk.practice1.hw;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

public class HWBJ37 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        int fetch = Integer.parseInt(scanner.nextLine());

        int m = Integer.parseInt(scanner.nextLine());

        List<List<Integer>> all = new ArrayList<>();
        int max = 0;
        for (int i = 0; i < m; i++) {
            int[] nums = Arrays.stream(scanner.nextLine().split(",")).mapToInt(Integer::parseInt).toArray();
            List<Integer> list = new ArrayList<>();
            for (int num : nums) {
                list.add(num);
            }
            all.add(list);
            max = Math.max(max,list.size());
        }

        StringBuffer sb = new StringBuffer();
        int index = 0;

        while (index < max) {
            for (List<Integer> list : all) {
                if (sb.length()>0 && !list.isEmpty()) {
                    sb.append(",");
                }
                int f = 0;
                while (f < fetch && !list.isEmpty()) {
                    sb.append(list.remove(0));
                    if (f!=fetch-1&&!list.isEmpty()) {
                        sb.append(",");
                    }
                    f++;
                }
            }
            index+=fetch;

        }
       System.out.println(sb);

    }


}
